<?php
/**
 * Created by PhpStorm.
 * User: huangpeng3
 * Date: 2018/1/5
 * Time: 下午1:39
 */
namespace Home\Controller;

use Think\Controller;

class LoginController extends Controller
{

    /**
     * 登录
     */
    public function login()
    {
        $user = D('Login');
        $obj = $user->checkuser($_POST['username'], $_POST['password']);
        echo json_encode($obj, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 注册
     */
    public function reg()
    {
        $reg = D('reg');
        $res = $reg->register($_POST['username'], $_POST['password']);
        echo json_encode($res, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 查询用户是否已经注册
     */
    public function exist(){
        $rst = array();
        $exist = D('Reg');
        $flag = $exist->exists($_REQUEST['username']);
        if($flag){
            $rst['status'] = true;
            $rst['info'] = '账号已经被注册';
            $rst['already_reg'] = 1;
        }
        else{
            $rst['status'] = false;
            $rst['info'] = '账号没有被注册';
            $rst['already_reg'] = 0;
        }
        echo json_encode($rst, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 退出
     */
    public function logout()
    {
        unset($_SESSION['user_info']);
        $rtn = array(
            'status'=>true,
            'info' => '登出成功',
        );
        echo json_encode($rtn, JSON_UNESCAPED_UNICODE);
    }
}